package com.briup.web;

import com.briup.pojo.Customer;
import com.briup.service.CustomerService;
import com.briup.serviceImp.CustomerServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class CustomerRegistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String zip = request.getParameter("zip");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        Customer customer = new Customer(null, username, password, zip, address, phone, email);
        CustomerService customerServiceImp = new CustomerServiceImp();
        if (customerServiceImp.existName(username)){
            customerServiceImp.regist(customer);
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {
            request.setAttribute("key1", "用户名已被占用");
            System.out.println("用户名已被占用");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}
